#include <time.h>
#include <wild/WildConfig.h>
#include <wild/GlobeTimer.h>

//using namespace std;

namespace wild
{

const Ulong GlobeTimer::CLOCK_PER_SECOND = 30;

//--------------------------------------------------------------------------------------
GlobeTimer::GlobeTimer()
{
	m_bTimerStopped     = true;


	m_llStopTime        = 0;
	m_llLastElapsedTime = 0;


	// Use QueryPerformanceFrequency to get the frequency of the counter
	//Ulong qwTicksPerSec = CLOCK_PER_SECOND;


}


//--------------------------------------------------------------------------------------
void GlobeTimer::Reset()
{
	Ulong qwTime = GetAdjustedCurrentTime();


	m_llLastElapsedTime = qwTime;
	m_llStopTime        = 0;
	m_bTimerStopped     = false;
}


//--------------------------------------------------------------------------------------
void GlobeTimer::Start()
{
	// Get the current time
	Ulong qwTime = 0;
	qwTime = GetAdjustedCurrentTime();



	m_llStopTime = 0;
	m_llLastElapsedTime = qwTime;
	m_bTimerStopped = false;
}


//--------------------------------------------------------------------------------------
void GlobeTimer::Stop()
{
	if( !m_bTimerStopped )
	{
		Ulong qwTime = GetAdjustedCurrentTime();
		m_llStopTime = qwTime;
		m_llLastElapsedTime = qwTime;
		m_bTimerStopped = true;
	}
}

//--------------------------------------------------------------------------------------
double GlobeTimer::GetElapsedTime()
{
	Ulong qwTime = GetAdjustedCurrentTime();

	double fElapsedTime = (double) ( qwTime - m_llLastElapsedTime ) / (double) CLOCK_PER_SECOND;
	m_llLastElapsedTime = qwTime;

	// See the explanation about clamping in GlobeTimer::GetTimeValues()
	if( fElapsedTime < 0.0f )
		fElapsedTime = 0.0f;

	return fElapsedTime;
}


//--------------------------------------------------------------------------------------
// If stopped, returns time when stopped otherwise returns current time
//--------------------------------------------------------------------------------------
Ulong GlobeTimer::GetAdjustedCurrentTime()
{
	return ::clock();
}


//--------------------------------------------------------------------------------------
bool GlobeTimer::IsStopped()
{
	return m_bTimerStopped;
}




}	//	namespace wild
